<!-- BEGIN PAGE HEADER-->
<?php $this->load->view('partials/breadcrumb.tpl') ?>
<!-- END PAGE HEADER-->
<style>
    body > div.page-container > div.page-content-wrapper > div > div.page-bar > div > div{
        display: none;
    }
</style>
<!-- BEGIN PAGE CONTENT-->
<script type="text/javascript">
    function defConfirm(url) {
        if (!confirm("确定要删除吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            $("select").val('');
            $("input[type='text']").val('');
        });
    });
</script>
<!-- loading层 -->
<style>
    .loading-x{
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: #0000008a;
        z-index: 20050;
    }
    .x-contain{
        margin: 18%;
        text-align: center;
    }
</style>
<div class="loading-x">
    <div class="x-contain">
        <img src="/static/assets/26073943_nCX5.gif" alt="">
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet">
            <?= form_open('', Array('class' => 'form-inline', 'role' => 'form', 'id' => "amazon_form")) ?>
            <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy/mm">
                <input type="text" class="form-control" name="month" value="<?= $month ?>" placeholder="统计月份" autoComplete="off">
            </div>

            <?= search_form_dropdown('shop_id', array('' => '选择店铺...') + $allShops, $shop_id, '所属店铺', 'class="form-control input-xs" id="shop-choose"'); ?>

            <?= search_form_dropdown('country', array('' => '选择国家...') + IOSS_StaticConfig::getCountryInfo(), $country, '国家', 'class="form-control input-xs"'); ?>

            <?= search_form_dropdown('coin_type', array('' => '选择币种...') + ['USD' => '美元','GBP' => '英镑','EUR' => '欧元','AUD' => '澳元', 'JPY' => '日元', 'CAD' => '加元'], $coin_type, '币种', 'class="form-control input-xs"'); ?>

            <?= search_form_dropdown('asin', array('' => '有无asin...',0 => '无', 1 => '有'), $asin, '有无asin', 'class="form-control input-xs"'); ?>

            <div style="clear:both;height:14px;"></div>
            <?= search_form_input('order_number_perpage', filterValue('order_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>

            <button type="button" class="btn default" id="reset">重置条件</button>
            <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>
            <button type="button" id="amazon_export" class="btn yellow-gold">导出 <i class="fa fa-table"></i></button>
            <a href="<?= site_url('analyse/order_export/static_info?month='.$month.'&&group_id='.$this->_user->group_id.'&&shop_id='.$shop_id.'&&country='.$country.'&&coin_type='.$coin_type) ?>" class="btn red">分析 <i class="fa fa-dollar"></i></a>
            <label style="float: right;margin-right: 20px" class="label label-danger">总计<?php echo $total_number ?>
                条
            </label>
            <?= form_close() ?>
            <script>
                $("#group-choose").change(function () {
                    var group_id = $(this).children('option:selected').val();
                    $.ajax({
                        url: "<?= site_url('shops/shop/getShopsByGroupId') ?>",
                        method: 'POST',
                        dataType: 'JSON',
                        data: {'group_id': group_id},
                        success: function (res) {
                            console.log(res);
                            var html = '<option value="" selected="selected">选择店铺...</option>';
                            var shop = $("#shop-choose");
                            for (i in res.data){
                                console.log(i)
                                html += '<option value="'+i+'">'+res.data[i]+'</option>'
                            }
                            shop.html(html)
                        },
                        fail: function (res) {
                            toastr.error(res.msg);
                        }
                    })
                })
            </script>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <form id="ajax_merge_form" action="" method="post" onsubmit="return false">
                                <input type="hidden" name="apply_number" id="apply_number">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead class="flip-content">
                                    <table class="table table-striped table-bordered table-hover">
                                        <thead class="flip-content">
                                        <tr>
                                            <th width="10%">日期</th>
                                            <th width="6%">asin</th>
                                            <th width="6%">sku</th>
                                            <th width="5%">所属组</th>
                                            <th width="6%">所属店铺</th>
                                            <th width="7%">北京时间</th>
                                            <th width="4%">类型</th>
                                            <th width="3%">币种</th>
                                            <th width="7%">订单号</th>
                                            <th width="4%">产品售价</th>
                                            <th width="3%">FBA费</th>
                                            <th width="3%">其他</th>
                                            <th width="3%">总计</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php foreach ($list as $v) { ?>
                                            <tr>
                                                <td><?= $v->date ?></td>
                                                <td class="text-center"><?= $v->asin?$v->asin: '<button data-sku="'.$v->sku.'" data-date="'.$month.'" class="btn blue asin-update">更新asin</button>'?></td>
                                                <td><?= $v->sku ?></td>
                                                <td><?= $v->group_name ?></td>
                                                <td><?= $v->shop_name ?></td>
                                                <td><?= date('Y-m-d H:i:s',strtotime($v->date)) ?></td>
                                                <td><?= $v->type ?></td>
                                                <td><?= $v->coin_type ?></td>
                                                <td><?= $v->order_id ?></td>
                                                <td><?= $v->product_sales ?></td>
                                                <td><?= $v->fba_fees ?></td>
                                                <td><?= $v->other ?></td>
                                                <td><?= $v->total ?></td>
                                            </tr>

                                        <?php } ?>
                                        </tbody>
                                    </table>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <script>
            $("#amazon_export").click(function () {
                $(".loading-x").fadeIn();
                var formData = $("#amazon_form").serialize();
                console.log(formData)
                $.ajax({
                    url: '<?= site_url('shops/order/python_export') ?>',
                    method: 'POST',
                    data: formData,
                    dataType: 'JSON',
                    success: function (res) {
                        $(".loading-x").fadeOut();
                        console.log(res);
                        if (res.status){
                            toastr.success(res.msg);
                            setTimeout(function () {
                                location.href = res.data
                            },1200)

                        } else {
                            toastr.error(res.msg)
                        }
                        console.log(res)
                    },
                    fail: function (res) {
                        console.log(res)
                    }
                })
            });

        </script>
        <!-- End: life time stats -->
    </div>
</div>
<div class="modal fade apply-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title apply-title">更新asin</h4>
            </div>
            <div class="modal-body">
                <form id="apply_form">
                    <div class="form-group">
                        <label class="control-label">需要更新的sku:</label>
                        <label id="edit_sku" class="label label-danger" style="font-size: 14px;margin-left: 12px;">加载中...</label>
                    </div>

                    <div class="form-group">
                        <label for="asin" class="control-label">asin:</label>
                        <input type="text" maxlength="10" required name="asin" class="form-control" id="asin">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary asin-submit">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    // 显示更新模态框
    $('.asin-update').click(function () {
        sku = this.dataset.sku;
        date = this.dataset.date;
        $("#edit_sku").html(sku);
        $('.apply-modal').modal();
        });

    // 确认更新asin
    $(".asin-submit").click(function () {
        $.ajax({
            url: "<?= site_url('shops/order/asin_update') ?>",
            method: 'POST',
            data: {'date': date,'sku': sku, 'asin': $("input[name='asin']").val()},
            dataType: 'POST',
            success: function (res) {
                console.log(res)
            },
            fail: function (res) {
                console.log(res)
            }
        })
    })
</script>
